﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for AdsControl.xaml
    /// </summary>
    public partial class HeadlineControl : UserControl
    {
        #region Public Properties
        private List<NewsSection> _listNewsSections;
        public List<NewsSection> ItemsSource
        {
            get { return _listNewsSections; }
            set
            {
                _listNewsSections = value;
                _InitializeControl();
            }
        }
        #endregion

        public HeadlineControl()
        {
            InitializeComponent();
        }

        private void _InitializeControl()
        {
            List<NewsSection> strippedList = new List<NewsSection>();
            foreach (NewsSection section in _listNewsSections)
            {
                List<NewsArticle> newsArticle = section.NewsArticles.Where(innerElement =>
                            !String.IsNullOrEmpty(innerElement.ImageUrl) &&
                            !String.IsNullOrEmpty(innerElement.Headline)).ToList();
                if (newsArticle.Count > 0)
                    strippedList.Add(new NewsSection() { NewsArticles = newsArticle });
            }
            if (strippedList.Count >= 3)
            {
                gridLeftNews.DataContext = new HeadlineDataItem() { Image = strippedList[0].NewsArticles[0].ImageUrl, Headline = strippedList[0].NewsArticles[0].Headline.ToUpper() };
                gridCenterNews.DataContext = new HeadlineDataItem() { Image = strippedList[1].NewsArticles[0].ImageUrl, Headline = strippedList[1].NewsArticles[0].Headline.ToUpper() };
                gridRightNews.DataContext = new HeadlineDataItem() { Image = strippedList[2].NewsArticles[0].ImageUrl, Headline = strippedList[2].NewsArticles[0].Headline.ToUpper() };
            }
            else
            {
                if (strippedList.Count >= 2)
                {
                    gridLeftNews.DataContext = new HeadlineDataItem() { Image = strippedList[0].NewsArticles[0].ImageUrl, Headline = strippedList[0].NewsArticles[0].Headline.ToUpper() };
                    if (strippedList[0].NewsArticles.Count > 1 || strippedList[1].NewsArticles.Count > 1)
                    {
                        if (strippedList[0].NewsArticles.Count > 1)
                        {
                            gridCenterNews.DataContext = new HeadlineDataItem() { Image = strippedList[0].NewsArticles[1].ImageUrl, Headline = strippedList[0].NewsArticles[1].Headline.ToUpper() };
                            gridRightNews.DataContext = new HeadlineDataItem() { Image = strippedList[1].NewsArticles[0].ImageUrl, Headline = strippedList[1].NewsArticles[0].Headline.ToUpper() };
                        }
                        else
                        {
                            gridCenterNews.DataContext = new HeadlineDataItem() { Image = strippedList[1].NewsArticles[0].ImageUrl, Headline = strippedList[1].NewsArticles[0].Headline.ToUpper() };
                            gridRightNews.DataContext = new HeadlineDataItem() { Image = strippedList[1].NewsArticles[1].ImageUrl, Headline = strippedList[1].NewsArticles[1].Headline.ToUpper() };
                        }
                    }
                    else
                    {
                        gridRightNews.DataContext = new HeadlineDataItem() { Image = strippedList[1].NewsArticles[0].ImageUrl, Headline = strippedList[1].NewsArticles[0].Headline.ToUpper() };
                        Random rand = new Random();
                        int section = rand.Next(0, _listNewsSections.Count());
                        int article = rand.Next(0, _listNewsSections[section].NewsArticles.Count());
                        gridCenterNews.DataContext = new HeadlineDataItem() { Image = _listNewsSections[section].NewsArticles[article].ImageUrl, Headline = _listNewsSections[section].NewsArticles[article].Headline.ToUpper() };
                    }
                }
                else
                {
                    List<HeadlineDataItem> headlineDataItems = new List<HeadlineDataItem>();
                    Random rand = new Random();
                    for (int i = 0; i < 3; i++)
                    {
                        int section = rand.Next(0, strippedList.Count());
                        Random rand1 = new Random(section);
                        int article = rand1.Next(0, strippedList[section].NewsArticles.Count());
                        headlineDataItems.Add(new HeadlineDataItem() { Image = strippedList[section].NewsArticles[article].ImageUrl, Headline = strippedList[section].NewsArticles[article].Headline.ToUpper() });
                    }
                    gridLeftNews.DataContext = headlineDataItems[0];
                    gridCenterNews.DataContext = headlineDataItems[1];
                    gridRightNews.DataContext = headlineDataItems[2];
                }
            }
        }
    }

    public class HeadlineDataItem
    {
        public string Image { get; set; }
        public string Headline { get; set; }
    }
}
